package data.set.adaptator;

import java.util.Iterator;


import data.mapper.Mapper;
import data.set.OfflineSet;
import data.set.OfflineSetIterator;


public class OfflineSet_Mapper<From,To> implements OfflineSet<To> {

	protected Mapper<From,To> mapper;
	protected OfflineSet<From> fromset;
	
	public OfflineSet_Mapper(Mapper<From,To> mapper,OfflineSet<From> fromset)
	{
		this.mapper=mapper;
		this.fromset=fromset;
	}
	
	public long size() {
		return fromset.size();
	}

	public To getObject(int id) {		
		return(mapper.map(fromset.getObject(id)));
	}

	public OfflineSetIterator<To> inputsetiterator() {
		return(new OfflineSetIterator_Mapper<From,To>(mapper,fromset.inputsetiterator()));
	}

	public Iterator<To> iterator() {
		return(new Iterator_Mapper<From,To>(mapper,fromset.iterator()));
	}

	@SuppressWarnings("hiding")
	private class OfflineSetIterator_Mapper<From,To> implements OfflineSetIterator<To> 
	{

		protected OfflineSetIterator<From> iterator;
		protected Mapper<From,To> mapper;
		
		public OfflineSetIterator_Mapper(Mapper<From,To> mapper,OfflineSetIterator<From> toit)
		{
			iterator=toit;
			this.mapper=mapper;
		}
		
		public int getCurrentId() {
			return(iterator.getCurrentId());
		}

		public To getCurrentObject() {
			return(mapper.map(iterator.getCurrentObject()));
		}

		public boolean hasNext() {
			return(iterator.hasNext());
		}

		public void next() {
			iterator.next();		
		}
	}
	
	@SuppressWarnings("hiding")
	private class Iterator_Mapper<From,To> implements Iterator<To> {

		protected Mapper<From,To> mapper;
		protected Iterator<From> to;
		
		public Iterator_Mapper(Mapper<From,To> mapper,Iterator<From> to)
		{
			this.mapper=mapper;
			this.to=to;
		}
		
		public boolean hasNext() 
		{
			return(to.hasNext());
		}

		public To next() {
			return mapper.map(to.next());
		}

		public void remove() {
			to.remove();		
		}

	}

}
